<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #4c4c4c;
        }
        .scan {
        	width: 216px;
        	height: 216px;
        	background-image: url('../image/scan.png');
        	background-size: 100% 100%;
        	background-repeat: no-repeat;
        	margin: 72px auto 30px;
        }
        .scan_p {
        	font-size: 16px;
        	color: #fff;
        	text-align: center;
            line-height: 40px;
        }
    </style>
</head>
<body>
    <div class="scan"></div>
    <!-- <div class="scan_p" tapmode onclick="toSelectFrame();">扫码遇到问题？</div> -->
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
    
    var needOpen = true; // 默认无需viewappear时打开扫码框

    apiready = function(){
        FNScanner = api.require('FNScanner');
        _w = api.frameWidth;
        w = _w/2 - 106;
        systype = api.systemType;
        /*if(systype == 'ios'){
            openIosScan();
        }else{
            openAndroidScan();
        }*/

        api.addEventListener({
            name:'changeNeedOpen'
        }, function(ret, err){    
            needOpen = true;
        });

        api.addEventListener({
            name:'resume'
        }, function(ret, err){    
            FNScanner.onResume();
        });
        api.addEventListener({
            name:'pause'
        }, function(ret, err){    
            FNScanner.onPause();
        });

        api.addEventListener({
            name: 'viewappear'
        }, function(ret, err){
            FNScanner = api.require('FNScanner');
            _w = api.frameWidth;
            w = _w/2 - 106;
            systype = api.systemType;
            if(systype == 'ios'){
                if(needOpen){
                    openIosScan();
                }
            }else{
                if(needOpen){
                    openAndroidScan();
                    /*var _w = api.winWidth;
                    var _h = api.winHeight;
                    var _height = _h*212/_w;
                    FNScanner.openView({
                        autorotation: false,
                        fixedOn:api.frameName,
                        fixed:false,
                        rect:{
                            x: w,
                            y: 74,
                            w: 212,
                            h: _height
                        }
                    }, function(ret, err) {
                            if (ret.eventType == 'success') {
                                FNScanner.closeView();
                                if(ret.content){
                                    toScanOrder(ret.content);
                                }
                                // alert(JSON.stringify(ret));
                            } else {
                                // alert(JSON.stringify(err));
                            }
                    });*/
                }
            }
        });
    };

    function toSelectFrame () {
        api.openWin({
            name: 'select_order',
            url: './select_order.html',
        });
    };

    function openAndroidScan () {
        var headerH = api.pageParam.headerH + 72 + 216;
        api.openFrame({
            name: 'scan_frame_bg',
            url: './scan_frame_bg.html',
            rect: {
                x: 0,
                y: headerH - 3,
                w: 'auto',
                h: api.winHeight - headerH - 20
            },
            fixedOn:api.frameName,
        });
        var _w = api.winWidth;
        var _h = api.winHeight;
        var _height = _h*212/_w;
        setTimeout(function(){
            FNScanner.openView({
                autorotation: false,
                fixedOn:api.frameName,
                fixed:false,
                rect:{
                    x: w,
                    y: 74,
                    w: 212,
                    h: _height
                }
            }, function(ret, err) {
                if (ret.eventType == 'success') {
                    FNScanner.closeView();
                    if(ret.content){
                        fnCheckStaff(ret.content);
                    }
                } else {
                    // alert(JSON.stringify(err));
                }
            });
        },300);
    }

    function openIosScan () {
        FNScanner.openView({
            autorotation: false,
            fixedOn:api.frameName,
            fixed:false,
            rect:{
                x: w,
                y: 74,
                w: 212,
                h: 212
            }
        }, function(ret, err) {
            if (ret.eventType == 'success') {
                FNScanner.closeView();
                if(ret.content){
                    // alert(ret.content);
                    fnCheckStaff(ret.content);
                }
                // alert(JSON.stringify(ret));
            } else {
                // alert(JSON.stringify(err));
            }
        });
    };

    function fnCheckStaff(content) {
        user = $api.getStorage('shop_data');
        openLoading();
        api.ajax({
            url: rootPath+'/api/seller_order/check_staff',
            method: 'post',
            data: {
                values: { 
                    content: content,
                    shop_id : user.id,
                    token : user.token
                },

            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 200){
                    var isSuccess = 1;
                    var order_id = ret.order_id;
                    api.sendEvent({
                        name: 'fnChangeGroupFrame',
                        extra: {
                            index_: 2, 
                        }
                    });
                }else{
                    var isSuccess = 0;
                    var order_id = 0;
                }
                api.openWin({
                    name: 'scan_result',
                    url: './scan_result.html',
                    pageParam: {
                        isSuccess: isSuccess,
                        orderId: order_id,
                    }
                });
            } else {
                
            }
            closeLoading();


        });
    }
</script>
</html>